package com.wodogs.utils.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.google.gson.Gson;
import com.wodogs.utils.bean.WfProxy;
import com.wodogs.utils.tools.HttpMry;
import com.wodogs.utils.tools.ProxyCheck;
import com.wodogs.utils.tools.ToolHttp;

public class ProxyService {
	
	private static String OK_LIST_URL = "http://localhost:89/mercury/proxy/okList/";
	
	private static String USE_URL = "http://localhost:89/mercury/proxy/use";
	
	private static String DISABLED_URL = "http://localhost:89/mercury/proxy/disabled";
	
	private static String LOGIN_URL = "";
	
//	private static String LOGIN_SUCCESS_URL = "http://www.bilibili.com/";
	
	public static void main(String[] args) {
		List<WfProxy> proxyList = ProxyService.getOkList("bilibili",LOGIN_URL,null,null, 20000);
		while(proxyList == null||proxyList.size()<20000){
			List<WfProxy> proxyList2 = ProxyService.getOkList("bilibili",LOGIN_URL,null,null, 2000);
			if(proxyList == null){
				proxyList = proxyList2;
			}else if(proxyList2!=null){
			    proxyList.addAll(proxyList2);
			}
		}
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<WfProxy> getOkList(String web_site,String test_url,String test_str,String last_proxy, Integer count){
		List<WfProxy> list = new ArrayList<WfProxy>();
		if(StringUtils.isBlank(web_site)){
			return list;
		}
		String url = OK_LIST_URL+web_site+"?";
		if(StringUtils.isNotBlank(last_proxy)){
			url += "last_proxy="+last_proxy;
		}
		if(count!=null){
			if(!url.endsWith("?")){
				url+="&";
			}
			url +=("count="+count.intValue());
		}
		String accountStr = ToolHttp.get(false, url);
    	try{
    		
    		Gson gson = new Gson();
    		List listCount = gson.fromJson(accountStr, List.class);
    		
    		list = new ProxyCheck().process(listCount, test_url, test_str, 10);
    	}catch(Exception ex){
    		System.err.println(ex.getMessage());
    		return list;
    	}
    	return list;
	}
	
	public static List<String> getIdList(List<WfProxy> list){
		if(list == null){
			return null;
		}
		List<String> idList = new ArrayList<String>();
		for(WfProxy p:list){
			idList.add(p.getId());
		}
		return idList;
	}
	
	public static List<WfProxy> getUnusedList(List<WfProxy> list){
		if(list == null){
			return null;
		}
		List<WfProxy> idList = new ArrayList<WfProxy>();
		for(WfProxy p:list){
			if(p.getIs_used().equals(0)){
				idList.add(p);
			}
		}
		return idList;
	}
	
	public static boolean use(String id){
		if(StringUtils.isBlank(id)){
			return false;
		}
    	
    	return HttpMry.get( USE_URL+"/"+id);
	}
	
	public static boolean disabled(String id){
		if(StringUtils.isBlank(id)){
			return false;
		}
		return HttpMry.get( DISABLED_URL+"/"+id);
	}
	
	public static DesiredCapabilities getCap(WfProxy p) {
		// String PROXY = "localhost:8080";//如果不是本机，localhost替换成IP地址
		//
		// org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
		// proxy.setHttpProxy(PROXY)
		// .setFtpProxy(PROXY)
		// .setSslProxy(PROXY);
		// DesiredCapabilities cap = new DesiredCapabilities();
		// cap.setPreference(CapabilityType.PROXY, proxy);

		String proxyIpAndPort = p.getIp()+":"+p.getPort();
		DesiredCapabilities cap = new DesiredCapabilities();
		Proxy proxy = new Proxy();
		proxy.setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort).setSslProxy(proxyIpAndPort);
		cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
		cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
		System.setProperty("http.nonProxyHosts", "localhost");
		cap.setCapability(CapabilityType.PROXY, proxy);
		return cap;
	}
}
